﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Money.Interfaces;
using Money.Data;
using One.Framework;
using Money.Entities;

namespace Money.Services
{
    public class AccountService : IAccountService
    {
        #region IAccountService Members

        private MoneyManagerDataContext context;

        public AccountService()
        {
            context = new MoneyManagerDataContext();
        }

        public void CreateAccount(Account item)
        {
            try
            {
                item.CreationDate = DateTime.Now;
                context.Accounts.InsertOnSubmit(item);
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot create account");
            }
        }

        public void DeleteAccount(int id)
        {
            try
            {
                Account item = GetAccountById(id);
                context.Accounts.DeleteOnSubmit(item);
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot delete account");
            }
        }

        public void UpdateAccount(Account item)
        {
            try
            {
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot update account");
            }
        }

        public Account GetAccountById(int id)
        {
            try
            {
                return context.Accounts.Where(a => a.AccountId == id).SingleOrDefault<Account>();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot get account");
            }
        }

        public List<Account> GetAccounts(string orderBy, string orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                var query = from u in context.Accounts
                            select u;
                if (orderBy == AccountOrderBy.AccountType1.ToString() || orderBy == AccountOrderBy.Currency.ToString())
                    orderBy += ".Name";
                return query.OrderByPaging<Account>(orderBy, orderDirection, page, pageSize, out totalRecords).ToList<Account>();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot get accounts");
            }
        }

        public List<Account> GetAccounts(int accountType, int currencyType, string orderBy, string orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                var query = from u in context.Accounts
                            where u.AccountType == accountType && u.CurrencyType == currencyType
                            select u;
                if (orderBy == AccountOrderBy.AccountType1.ToString() || orderBy == AccountOrderBy.Currency.ToString())
                    orderBy += ".Name";
                return query.OrderByPaging<Account>(orderBy, orderDirection, page, pageSize, out totalRecords).ToList<Account>();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot get accounts");
            }
        }

        #endregion
    }
}
